<?php
set_time_limit(0);
//ini_set("display_errors", "On");
//error_reporting(E_ALL);
require_once './Classes/PHPExcel.php';
// SELECT a.* FROM `u3` a, u3 b WHERE a.id != b.id  and a.name=b.name;
$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '得分')
            ->setCellValue('B1', '姓名')
            ->setCellValue('C1', '手机号')
            ->setCellValue('D1', '身份证')
            ->setCellValue('E1', '地区')
            ->setCellValue('F1', '学校')  
;
include '../c/config.php';

$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}


$shanghui=['徐汇区','杨浦区','七宝小学联合体','金山区','嘉定区','奉贤区','宝山区','崇明区','普陀区','松江区','浦东新区','虹口区','长宁区','闵行区','青浦区','静安区','黄浦区'];
$shangZimu=['xuhui','yangpu','qibaoxiaoxue','jinshan','jiading','fengxian','baoshan','chongming','putuo','songjiang','pudongxinqu','hongkou','changning','minhang','qingpu','jingan','huangpu'];
 
$nanhui=['启东市','如东县','如皋市','崇川区','市直代管','开发区代管','海安市','海门市','港闸区','通州区','通州湾江海联动开发示范区'];
$nanZimu=['qidongshi','rudongxian','runieshi','chongchuanqu','shizhidaiguan','kaifaqudaiguan','haianshi','haineishi','gangzhaqu','tongzhouqu','tongzhouwanhailiandongkaifa'];
 
 
$sql="SELECT * FROM `uinfo` WHERE nianji=5 AND quchong is NULL AND fen>0 order by fen  desc ";
$query =$mysqli->query($sql);
$userD=array();
$index=2;
while ($list=$query->fetch_array()) {
        
        $school= $list['school'];
        if (!empty($list['school2'])) {
        	$school.='-'.$list['school2'];
        }
        $qu='';
		if ('shanghai' == $list['qu'] ) {
          $qu='上海-';
          foreach ($shangZimu as $k => $v) {
          	 if($v == $list['hui']){
                 $qu.=$shanghui[$k];
          	 }
          }
		}else{
		  $qu='南通-';
 
          foreach ($nanZimu as $k => $v) {

          	 if($v == $list['hui']){
 
          	  
                 $qu.=$nanhui[$k];
          	 }
          }
		}
        

	    $objPHPExcel->setActiveSheetIndex(0)
	    ->setCellValue('A'.$index, $list['fen'])
	    ->setCellValue('B'.$index, $list['name'])
	    ->setCellValue('C'.$index, $list['phone'])
	    ->setCellValue('D'.$index, $list['idcard'].' ')
	    ->setCellValue('E'.$index, $qu)
	    ->setCellValue('F'.$index, $school)

	    ;
	    
	   
	    $index++;
	 
}
 
 
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('用户信息');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=五年级学生信息.xlsx');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');

// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;
